HackerRank Beautiful Pairs
https://www.hackerrank.com/challenges/beautiful-pairs/problem
提出
2/8 test cases failed :(
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
#
# Complete the 'beautifulPairs' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY A
# 2. INTEGER_ARRAY B
#
def beautifulPairs(A, B):
# Write your code here
ans = 0
for i in A:
if i in B:
ans += 1
B.remove(i)
if (ans == n):
return ans
else:
if (len(B) > 0):
return ans + 1
else:
return ans
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
n = int(input().strip())
A = list(map(int, input().rstrip().split()))
B = list(map(int, input().rstrip().split()))
result = beautifulPairs(A, B)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
#
# Complete the 'beautifulPairs' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY A
# 2. INTEGER_ARRAY B
#
'''
A 10 11 12 5 14
B 8 9 11 11 5
'''
def beautifulPairs(A, B):
a = Counter(A)
# print(a)
# Counter({10: 1, 11: 1, 12: 1, 5: 1, 14: 1})
b = Counter(B)
# print(b)
# Counter({11: 2, 8: 1, 9: 1, 5: 1})
ans = 0
for num in a.keys():
if num in b:
ans += min(anum, bnum)
# corner case
# must change 1 element
# A 1 1 1
# B 1 1 1
if ans == len(A):
return ans - 1
return ans + 1
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
n = int(input().strip())
A = list(map(int, input().rstrip().split()))
B = list(map(int, input().rstrip().split()))
result = beautifulPairs(A, B)
fptr.write(str(result) + '\n')
fptr.close()
提出
2/8 test cases failed :(
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
import collections
#
# Complete the 'beautifulPairs' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY A
# 2. INTEGER_ARRAY B
#
def beautifulPairs(A, B):
# Write your code here
# print(set(A) & set(B))
# print(collections.Counter(A), collections.Counter(B))
# Counter({1: 1, 2: 1, 3: 1, 4: 1}) Counter({3: 2, 1: 1, 2: 1})
AC = collections.Counter(A)
BC = collections.Counter(B)
# print(AC31)
ans = 0
for v in BC:
if ACv:
if ACv < BCv:
ans += ACv
BCv -= ACv
elif ACv > BCv:
ans += BCv
ACv -= BCv
else:
ans += ACv
ACv = 0
BCv = 0
restA = set()
restB = set()
for v in AC:
if ACv:
restA.add(v)
for v in BC:
if BCv:
restB.add(v)
if len(restA) > 0 and len(restB) > 0 and len(restA - restB) > 0:
ans += 1
return ans
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
n = int(input().strip())
A = list(map(int, input().rstrip().split()))
B = list(map(int, input().rstrip().split()))
result = beautifulPairs(A, B)
fptr.write(str(result) + '\n')
fptr.close()
提出
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'beautifulPairs' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY A
# 2. INTEGER_ARRAY B
'''
A 10 11 12 5 14
B 8 9 11 11 5
- sort
A 5 10 11 12 14
B 5 8 9 11 11
5 10 11 12 14
5 8 9 11 11
while A0 > b0: b.pop(0) <- difficlt to swith wheather to pop
'''
def beautifulPairs(A, B):
# Write your code here
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
n = int(input().strip())
A = list(map(int, input().rstrip().split()))
B = list(map(int, input().rstrip().split()))
result = beautifulPairs(A, B)
fptr.write(str(result) + '\n')
fptr.close()